package jp.co.showbiz.common.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipUtil {

    /**
     * @param filePath
     * @throws IOException
     */
    // public static boolean unZip(String filePath) throws IOException{
    //		
    // boolean flag = false;
    // File zipFileObj = new File(filePath);
    // if (!zipFileObj.exists()){
    // throw new IOException("zip file doesn't exist!!");
    // }
    //	
    // int index = filePath.lastIndexOf(".");
    // if (index == -1){
    // throw new IOException("Invalid zip File");
    // }
    // String extention = filePath.substring(index);
    // if (!".zip".equals(extention)){
    // throw new IOException("Invalid zip File");
    // }
    // try{
    // ZipInputStream input = new ZipInputStream(new FileInputStream(zipFileObj));
    // ZipEntry zipEntry = null;
    // byte[] buf = new byte[1024];
    // int c = 0;
    // while ((zipEntry = input.getNextEntry()) != null){
    // if (zipEntry.getName().equals("Thumbs.db")){
    // continue;
    // }
    // String parentPath = new File(filePath).getParentFile().getPath();
    // File file = new File(parentPath + "/" + zipEntry.getName());
    // if (!file.getParentFile().exists()){
    // file.getParentFile().mkdirs();
    // }
    //				
    // BufferedOutputStream output = new BufferedOutputStream(
    // new FileOutputStream(file));
    // while ((c = input.read(buf)) != -1){
    // output.write(buf, 0, c);
    // }
    // output.close();
    // File unZipFile = new File(file.getPath());
    // if (!unZipFile.exists()){
    // throw new IOException("unZip error");
    // }
    // unZipFile.setLastModified(zipEntry.getTime());
    // }
    // input.close();
    // flag = true;
    // }catch (ClassFormatError e){
    // flag = true;
    // }catch (IOException e){
    //			
    // throw new IOException(e.toString());
    // }
    //		
    // return flag;
    // }
    public static boolean unZip(String zipFilePath) throws IOException {
        boolean flag = false;
        File file = new File(zipFilePath);
        // System.out.println("path: " + file.getAbsolutePath());
        if (!file.exists()) {
            throw new IOException(zipFilePath + " zip file doesn't exist!!");
        }
        int delIndex = file.getName().lastIndexOf(".");
        String extensionName = file.getName().substring(delIndex);
        if (!extensionName.equals(".zip")) {
            throw new IOException("Invalid zip File");
        }

        String basePath = file.getParentFile().getPath() + "/";
        ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
        byte[] buf = new byte[1024];
        int size = 0;
        ZipEntry entry = null;
        while ((entry = zipInput.getNextEntry()) != null) {
            File unZipFile = new File(basePath + entry.getName());
            if (entry.isDirectory()) {
                if (!unZipFile.mkdirs()) {
                    flag = false;
                    throw new IOException(unZipFile.getPath() + " => Has Directory in it => should bu removed");
                }
            } else {
                unZipFile.getParentFile().mkdirs();
                BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(unZipFile));
                while ((size = zipInput.read(buf)) != -1) {
                    output.write(buf, 0, size);
                }
                output.close();
            }
        }
        zipInput.close();

        File[] unZipFiles = file.getParentFile().listFiles();
        if (null != unZipFiles && unZipFiles.length > 0) {
            flag = true;
        } else {
            flag = false;
        }
        return flag;
    }

    /**
     * @param uploadedFile
     * @return
     * @throws IOException
     */
    public static boolean unZip(File uploadedFile) throws IOException {

        return unZip(uploadedFile.getPath());
    }

}
